﻿
Imports NearAop

<AddProxyMethod(GetType(Proxy2), IsUpdateValue:=True, IsSetValueOnExit:=True)>
Public Class T05_实例方法代理
    Private value1 As Integer
    Private value2 As Integer

    Public Sub SetValue(value1 As Integer, value2 As Integer)
        Me.value1 = value1
        Me.value2 = value2
    End Sub

    Public Function GetValue(value1 As Integer, value2 As Integer) As String
        Return $"{Me.value1} . {Me.value2} . {value1} . {value2}"
    End Function


    <SetCancelAop>'取消AOP操作
    Public Sub Test()
        SetValue(1, 2)
        Console.WriteLine(GetValue(3, 4))
    End Sub

End Class


<AddProxyMethod(GetType(Proxy2), IsUpdateValue:=True, IsSetValueOnExit:=True)>
Public Class T05_静态方法
    Private Shared value1 As Integer
    Private Shared value2 As Integer

    Public Shared Sub SetValue(value1 As Integer, value2 As Integer)
        T05_静态方法.value1 = value1
        T05_静态方法.value2 = value2
    End Sub

    Public Shared Function GetValue(value1 As Integer, value2 As Integer) As String
        Return $"{T05_静态方法.value1} . {T05_静态方法.value2} . {value1} . {value2}"
    End Function


    <SetCancelAop>'取消AOP操作
    Public Shared Sub Test()
        SetValue(1, 2)
        Console.WriteLine(GetValue(3, 4))
    End Sub

End Class


Public Class Proxy2
    Implements IAopHandlerMethod
    Public Sub OnEntry(methodArgs As MethodArgs) Implements IAopHandlerMethod.OnEntry
        '--获取参数的值
        '通过索引获取指定位置的参数值
        Dim value1 = methodArgs.TryGetValue(Of Integer)(0)
        '通过参数名称获取参数值
        Dim value2 = methodArgs.TryGetValue(Of Integer)("value2")

        '修改参数值
        '通过索引修改指定位置的的参数值
        methodArgs.TrySetValue(0, 192)
        '通过参数名称修改参数值
        methodArgs.TrySetValue("value2", 168)


        '获取全部的参数值(包含被修改后的值,内容取决于读取时机)
        Dim values = String.Join(",", methodArgs.ParamValues)
        '获取方法名
        Dim methodName = $"{methodArgs.MethodInfo.DeclaringType}.{methodArgs.MethodInfo.Name}"
        Console.WriteLine($"进入[{methodName}]方法时, value1 = {value1} , value2 = {value2}, 全部参数值 = {values}")
    End Sub


    Public Function OnExit(Of TResult)(methodArgs As MethodArgs, result As TResult) As TResult Implements IAopHandlerMethod.OnExit
        '获取方法名
        Dim methodName = $"{methodArgs.MethodInfo.DeclaringType}.{methodArgs.MethodInfo.Name}"
        '获取全部的参数值(包含被修改后的值,内容取决于读取时机)
        Dim values = String.Join(",", methodArgs.ParamValues)
        Console.WriteLine($"退出[{methodName}]方法时, 全部参数值 = {values}")
        Return CObj($"{methodName}方法返回 : {result}")
    End Function
End Class